package	com.mes.web.controller.check;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.check.RejectDeal;
import com.mes.entity.tdd.Rejects;
import com.mes.entity.tdd.Staff;
import com.mes.service.check.RejectDealService;
import com.mes.utils.DateJsonValueProcessor;
/**
 *
 *@ClassName: RejectDealController
 *@Description: 不合格品处置
 *@Author tdd
 *@Date 2019-05-29 11:12:45
 *@version 1.0
*/
@Controller
@RequestMapping("/rejectDeal")
public class RejectDealController{
	@Resource RejectDealService rejectDealService;
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public RejectDeal queryById(Long id) {
		RejectDeal param = rejectDealService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<RejectDeal> queryAllByMution(RejectDeal param) {
		List<RejectDeal> list = rejectDealService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(RejectDeal param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<RejectDeal> list = rejectDealService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = rejectDealService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("rows", jsonArray);
		result.put("total", total);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(result.toString());
		out.flush();
		out.close();
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(RejectDeal param,Long jid, HttpServletResponse response,HttpServletRequest request) throws Exception{
		//判断session
        HttpSession session  = request.getSession();
        //从session中取出用户身份信息
        Staff userSession = (Staff) session.getAttribute("staff");
        param.setSid(userSession.getStaffid());
		Long id = rejectDealService.insert(param);
		int fg = 0;
		if(null != id){
			Rejects rejects = new Rejects();
			rejects.setId(jid);
			rejects.setNum(param.getNum()+"");;
			rejectDealService.reduceRejects(rejects);
			fg = 1;
		}
		response.getWriter().print(fg);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/plInsert")
	public void plInsert(Long[] jid,Long[] pid,int[] num,String[] opinion,String[] code,HttpServletResponse response,HttpServletRequest request) throws Exception{
		int fg = 0;
		HttpSession session  = request.getSession();
		//从session中取出用户身份信息
		Staff userSession = (Staff) session.getAttribute("staff");
		Long staffid = userSession.getStaffid();
		for (int i = 0; i < jid.length; i++) {
			RejectDeal param = new RejectDeal();
			param.setPid(pid[i]);
			param.setNum(num[i]);
			param.setOpinion(opinion[i]);
			param.setCode(code[i]);
			//判断session
			param.setSid(staffid);
			Long id = rejectDealService.insert(param);
			if(null != id){
				Rejects rejects = new Rejects();
				rejects.setId(jid[i]);
				rejects.setNum(param.getNum()+"");;
				rejectDealService.reduceRejects(rejects);
				fg += 1;
			}
		}
		response.getWriter().print(fg);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(RejectDeal param,HttpServletResponse response) throws Exception{
		Integer count = rejectDealService.update(param);
		response.getWriter().print(count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = rejectDealService.delete(ids);
		response.getWriter().print(count);
	}
	/** 
	 * 查询不良品产品列表
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/allRejects")
	public void allRejects(Rejects rejects,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<Rejects> list = rejectDealService.allRejects(rejects);
		/*
		 * 条件查询总条数
		 */
		Integer total = list.size();
		/*
		 * 转为json数据传至页面
		 */
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("rows", jsonArray);
		result.put("total", total);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(result.toString());
		out.flush();
		out.close();
	}
}
